#include "AutobahnWS.h"

void AutobahnWS::begin(WebSocketsClient &client, const char *host, uint16_t port, char *realm, const char *url, const char *protocol)
{

	using namespace std::placeholders; // for `_1`
	this->_client = &client;
	this->setrealm(realm);
	this->_client->onEvent(std::bind(&AutobahnWS::webSocketEvent, this, _1, _2, _3));
	this->_client->setReconnectInterval(5000);
	this->_client->begin(host, port, url, protocol);
}
void AutobahnWS::beginssl(WebSocketsClient &client, const char *host, uint16_t port, char *realm, const char *url, const char *protocol)
{

	// using namespace std::placeholders; // for `_1`
	// this->_client = &client;
	// this->setrealm(realm);
	// this->_client->onEvent( std::bind(&AutobahnWS::webSocketEvent , this, _1,_2,_3));
	// this->_client->setReconnectInterval(5000);
	// this->_client->beginSSL(host, port, url, protocol);
}
void AutobahnWS::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
{

	switch (type)
	{
	case WStype_DISCONNECTED:
		//_websocketstate = type;
		//	_connected = false;
		core_debug("[WSc] Disconnected!\n");
		break;
	case WStype_CONNECTED:
		core_debug("[WSc] WStype_CONNECTED!\n");
		this->wampstatemachine(WAMP_MSGCODE_HELLO, payload, length);
		break;
	case WStype_TEXT:
		wampstatemachine(payload, length);
		break;
	case WStype_BIN:
	default:
		break;
	}
}
void AutobahnWS::send_towire(const char *payload)
{
	//core_debug("[send_towire] %s\n",payload);
	this->_client->sendTXT(payload);
}
void AutobahnWS::on_join(t_cb cb)
{
	onjoin_cb = cb;
}
void AutobahnWS::notify_welcome()
{
	// core_debug("on_join!\n");
	if (onjoin_cb != nullptr)
		onjoin_cb();
}

void AutobahnWS::loop()
{
	this->_client->loop();
}